package com.niit.galgame.service.impl;

import com.niit.dao.CollectDao;
import com.niit.dao.GameDao;
import com.niit.dao.UpvoteDao;
import com.niit.dao.UserDao;
import com.niit.entity.Collect;
import com.niit.entity.Game;
import com.niit.entity.Upvote;
import com.niit.entity.User;
import com.niit.galgame.service.VoteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class VoteServiceImpl implements VoteService {
    @Resource
    private CollectDao collectDao;
    @Resource
    private UpvoteDao upvoteDao;
    @Resource
    private GameDao gameDao;
    @Resource
    private UserDao userDao;

    @Override
    public int upovteGame(int gid, int uid) {
        System.out.println(gid+"："+uid);
        Game game = gameDao.queryById(gid);
        Upvote upvote = upvoteDao.queryOne(uid,gid);
        if(upvote != null){
            game.setUpvote(game.getUpvote()-1);
            gameDao.upvote(game);
            upvoteDao.deleteOne(uid,gid);
            return -1;
        }
        game.setUpvote(game.getUpvote()+1);
        gameDao.upvote(game);
        upvote = new Upvote(gid,uid);
        upvoteDao.insert(upvote);
        return 1;
    }

    @Override
    public int collectGame(int gid, int uid) {
        Collect collect = collectDao.queryOne(uid,gid);
        Game game = gameDao.queryById(gid);
        if (collect != null){
            game.setCollect(game.getCollect()-1);
            gameDao.collect(game);
            collectDao.deleteOne(uid,gid);
            return -1;
        }
        game.setCollect(game.getCollect()+1);
        gameDao.collect(game);
        collect = new Collect(uid,gid);
        collectDao.insert(collect);
        return 1;
    }

    @Override
    public Upvote queryUpvote(int gid, int uid) {
        return upvoteDao.queryOne(uid,gid);
    }

    public Collect queryCollect(int gid,int uid){
        return collectDao.queryOne(uid,gid);
    }

    @Override
    public User queryUser(int uid) {
        return userDao.queryById(uid);
    }
}
